//
//  SJSetGenderViewController.m
//  WSL
//
//  Created by CJ on 2018/2/2.
//  Copyright © 2018年 sanjuetech. All rights reserved.
//

#import "SJSetGenderViewController.h"
#import "SJUserInfoItem.h"
#import "SJSetUserInfoApi.h"

@interface SJSetGenderViewController ()

@property (nonatomic, strong) UIView *navigationBarView;
@property (nonatomic, strong) UIButton *maleBG;
@property (nonatomic, strong) UIButton *femaleBG;

@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation SJSetGenderViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [SJStatisticTool beginLogPageView:NSStringFromClass([self class])];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [SJStatisticTool endLogPageView:NSStringFromClass([self class])];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    if ([self.myInfo.gender isEqualToString:@"1"]) {
        self.selectedButton = _maleBG;
    } else {
        self.selectedButton = _femaleBG;
    }
    
    [self action:self.selectedButton];
}


- (void)setupUI {
    
    [self.view addSubview:self.navigationBarView];
    
    if (812 == SJScreenH) {
        self.navigationBarView.sd_layout
        .leftSpaceToView(self.view, SJLayoutFrame(0))
        .topSpaceToView(self.view, SJLayoutFrame(40))
        .heightIs(SJLayoutFrame(50))
        .widthIs(SJScreenW);
    } else {
        self.navigationBarView.sd_layout
        .leftSpaceToView(self.view, SJLayoutFrame(0))
        .topSpaceToView(self.view, SJLayoutFrame(0))
        .heightIs(SJLayoutFrame(50))
        .widthIs(SJScreenW);
    }
    
    [self.view addSubview:self.maleBG];
    self.maleBG.sd_layout
    .centerXEqualToView(self.view)
    .topSpaceToView(self.navigationBarView, SJMargin)
    .widthIs(SJLayoutFrame(361))
    .heightIs(SJLayoutFrame(39));
    
    [self.view addSubview:self.femaleBG];
    self.femaleBG.sd_layout
    .centerXEqualToView(self.view)
    .topSpaceToView(self.maleBG, SJMargin)
    .widthIs(SJLayoutFrame(361))
    .heightIs(SJLayoutFrame(39));
    
}


#pragma mark - gerter

- (UIView *)navigationBarView{
    
    if (!_navigationBarView) {
        
        _navigationBarView = [UIView new];
        _navigationBarView.backgroundColor = [UIColor clearColor];
        
        // 返回
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"addtext-icon-return"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_navigationBarView addSubview:backButton];
        backButton.sd_layout
        .centerYEqualToView(_navigationBarView)
        .leftSpaceToView(_navigationBarView, SJLayoutFrame(10))
        .heightIs(SJLayoutFrame(30))
        .widthIs(SJLayoutFrame(30));
        
        // 词条输入
        UILabel *wordLabel = [UILabel new];
        wordLabel.textColor = SJColorHexString(@"#ffffff");
        wordLabel.font = [UIFont fontWithName:@"Helvetica" size:SJLayoutFrame(17)];
        wordLabel.textAlignment = NSTextAlignmentCenter;
        wordLabel.text = @"性别";
        
        [_navigationBarView addSubview:wordLabel];
        wordLabel.sd_layout
        .centerYEqualToView(_navigationBarView)
        .centerXEqualToView(_navigationBarView)
        .heightIs(SJLayoutFrame(27));
        [wordLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW * .5];
        
        // 确定
        UIButton *affirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [affirmButton setImage:[UIImage imageNamed:@"personal_icon_completed"] forState:UIControlStateNormal];
        [affirmButton addTarget:self action:@selector(affirmAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_navigationBarView addSubview:affirmButton];
        affirmButton.sd_layout
        .centerYEqualToView(_navigationBarView)
        .rightSpaceToView(_navigationBarView, SJLayoutFrame(20))
        .heightIs(SJLayoutFrame(30))
        .widthIs(SJLayoutFrame(60));
        
    }
    return _navigationBarView;
}


- (void)affirmAction {
    
    // 1.拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"token"] = [SJUserDefaults objectForKey:SJToken];
    parameters[@"gender"] = [NSString stringWithFormat:@"%ld", (long)self.selectedButton.tag];
    
    SJSetUserInfoApi *api = [[SJSetUserInfoApi alloc] initWithParameters:parameters];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        if ([api.responseObject[@"result"][@"err"] intValue] == 0) {
            
            [SJUserDefaults setObject:api.responseObject[@"content"] forKey:SJUserInfo];
            [SJUserDefaults synchronize];
            
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                [self backAction];
            });
            
        } else {
            
            NSString *msg = api.responseObject[@"result"][@"msg"];
            [SVProgressHUD showErrorWithStatus:msg];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            
        }
        
        
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        attributes[@"name"] = @"设置性别";
        [SJStatisticTool event:@"setGender" attributes:attributes];
        
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)action:(UIButton *)button {
    
    self.selectedButton.layer.borderColor = [UIColor clearColor].CGColor;//边框颜色
    button.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
    self.selectedButton = button;
    
}

- (UIButton *)maleBG {
    
    if (!_maleBG) {
        
        _maleBG = [UIButton buttonWithType:UIButtonTypeCustom];
        [_maleBG addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        _maleBG.sd_cornerRadius = @(SJCornerRadius);
        _maleBG.backgroundColor = SJColorHexString(@"#ffffff19");
        _maleBG.layer.borderColor = [UIColor clearColor].CGColor;//边框颜色
        _maleBG.layer.borderWidth = SJLayoutFrame(1);//边框宽度
        _maleBG.tag = 1;
        
        UIImageView *rightImage = [UIImageView new];
        rightImage.image = [UIImage imageNamed:@"personal_icon_gender_male"];
        
        [_maleBG addSubview:rightImage];
        rightImage.sd_layout
        .centerYEqualToView(_maleBG)
        .rightSpaceToView(_maleBG, SJLayoutFrame(15))
        .widthIs(SJLayoutFrame(20))
        .heightIs(SJLayoutFrame(20));
        
        UILabel *leftLabel = [UILabel new];
        leftLabel.textColor = SJColorHexString(@"#ffffff");
        leftLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        leftLabel.textAlignment = NSTextAlignmentLeft;
        leftLabel.text = @"男";
        
        [_maleBG addSubview:leftLabel];
        leftLabel.sd_layout
        .centerYEqualToView(_maleBG)
        .leftSpaceToView(_maleBG, SJLayoutFrame(15))
        .widthIs(SJLayoutFrame(20))
        .heightIs(SJLayoutFrame(20));
        
    }
    return _maleBG;
}

- (UIButton *)femaleBG {
    
    if (!_femaleBG) {
        
        _femaleBG = [UIButton buttonWithType:UIButtonTypeCustom];
        [_femaleBG addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        _femaleBG.sd_cornerRadius = @(SJCornerRadius);
        _femaleBG.backgroundColor = SJColorHexString(@"#ffffff19");
        _femaleBG.layer.borderColor = [UIColor clearColor].CGColor;//边框颜色
        _femaleBG.layer.borderWidth = SJLayoutFrame(1);//边框宽度
        _femaleBG.tag = 2;
        
        UIImageView *rightImage = [UIImageView new];
        rightImage.image = [UIImage imageNamed:@"personal_icon_gender_female"];
        
        [_femaleBG addSubview:rightImage];
        rightImage.sd_layout
        .centerYEqualToView(_femaleBG)
        .rightSpaceToView(_femaleBG, SJLayoutFrame(15))
        .widthIs(SJLayoutFrame(20))
        .heightIs(SJLayoutFrame(20));
        
        UILabel *leftLabel = [UILabel new];
        leftLabel.textColor = SJColorHexString(@"#ffffff");
        leftLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        leftLabel.textAlignment = NSTextAlignmentLeft;
        leftLabel.text = @"女";
        
        [_femaleBG addSubview:leftLabel];
        leftLabel.sd_layout
        .centerYEqualToView(_femaleBG)
        .leftSpaceToView(_femaleBG, SJLayoutFrame(15))
        .widthIs(SJLayoutFrame(20))
        .heightIs(SJLayoutFrame(20));
    
    }
    return _femaleBG;
}

@end
